package com.heima.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.heima.account.entity.Account;
import com.heima.account.mapper.AccountMapper;
import com.heima.account.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-09-22
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Override
    public void deduct(Integer userId, Integer money) {
        log.info("开始扣款");
        try {

            // Wrapper<Account> query = new QueryWrapper<>();
            // ((QueryWrapper<Account>) query).eq("user_id",userId);
            // this.list(query);
            //
            //
            // LambdaQueryWrapper<Account> query1 = new LambdaQueryWrapper<>();
            // query1.eq(Account::getUserId,userId);
            // this.list(query1);

            LambdaUpdateWrapper<Account> update = new LambdaUpdateWrapper<>();
            // update tb_account set money = money - money where user_id = ?
            update.eq(Account::getUserId, userId);
            update.setSql("money = money - " + money);
            this.update(update);
        } catch (Exception e) {
            throw new RuntimeException("扣款失败，可能是余额不足！", e);
        }
        log.info("扣款成功");
    }



}
